package com.example.house

import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.databinding.ActivityHouseAddBinding
import com.example.house.entity.HouseAddBody
import com.example.house.entity.ProcessingStep
import com.example.house.vm.HouseAddViewModel
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/house/HouseAddActivity")
@AndroidEntryPoint
class HouseAddActivity : BaseMvvmActivity<ActivityHouseAddBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_house_add
    private val houseAddViewModel : HouseAddViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.list.setOnClickListener {
            ARouter.getInstance().build( "/house/HouseApplyActivity").navigation()
        }
        mDataBinding.back.setOnClickListener {
            finish()
        }
        mDataBinding.btnAdd.setOnClickListener {
            val houseAddBody = HouseAddBody(

             applicationArea = mDataBinding.applicationArea.text.toString().toInt(),
             applicationNumber = "AAA${mDataBinding.applicationArea.text}",
             contactMethod= mDataBinding.contactMethod.text.toString().toLong(),
             contactName= mDataBinding.contactName.text.toString(),
             documents= listOf("1","2"),
             enterpriseName= mDataBinding.enterpriseName.text.toString(),
             enterpriseProfileId= 1,
             entryPersonnel= mDataBinding.entryPersonnel.text.toString().toInt(),
             entryTime= mDataBinding.entryTime.text.toString(),
             id= 0,
             processingSteps= listOf(ProcessingStep( mDataBinding.entryTime.text.toString())),
             status= "新建",
            )
            houseAddViewModel.postHouseAdd(houseAddBody)
        }
        mDataBinding.entryTime.setOnClickListener {
            showTimePicker()
        }
        houseAddViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        houseAddViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }

    }

    private fun showTimePicker() {
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        val timePicker = MaterialTimePicker.Builder()
            .setTimeFormat(TimeFormat.CLOCK_24H)
            .setHour(hour)
            .setMinute(minute)
            .setTitleText("选择时间")
            .build()

        timePicker.addOnPositiveButtonClickListener {
            val selectedHour = timePicker.hour
            val selectedMinute = timePicker.minute
            val timeString = String.format("%02d:%02d", selectedHour, selectedMinute)
            mDataBinding.entryTime.setText(timeString)
        }

        timePicker.show(supportFragmentManager, "time_picker")
    }

}